package com.irdstudio.efp.riskm.service.impl;

import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.basic.framework.core.util.UUIDUtil;
import com.irdstudio.efp.console.common.ConsoleConstant;
import com.irdstudio.efp.flow.common.vo.PageApproveVO;
import com.irdstudio.efp.flow.service.facade.BizEventInfoService;
import com.irdstudio.efp.riskm.common.RiskColltConstant;
import com.irdstudio.efp.riskm.common.RiskwarningConstant;
import com.irdstudio.efp.riskm.service.dao.PspCheckRptDao;
import com.irdstudio.efp.riskm.service.dao.PspCheckTaskAppDao;
import com.irdstudio.efp.riskm.service.dao.PspSpecCheckCusDao;
import com.irdstudio.efp.riskm.service.domain.PspCheckRpt;
import com.irdstudio.efp.riskm.service.domain.PspCheckTaskApp;
import com.irdstudio.efp.riskm.service.domain.PspSpecCheckCus;
import com.irdstudio.efp.riskm.service.facade.PspCheckTaskAppApprService;
import com.irdstudio.efp.riskm.service.vo.PspCheckTaskAppVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Objects;
@Service("pspCheckTaskAppApprService")
public class PspCheckTaskAppApprServiceImpl implements PspCheckTaskAppApprService, FrameworkService {
    private static Logger logger = LoggerFactory.getLogger(PspCheckTaskAppApprServiceImpl.class);

    @Autowired
    private PspCheckTaskAppDao pspCheckTaskAppDao;

    @Autowired
    private PspSpecCheckCusDao pspSpecCheckCusDao;

    @Autowired
    private PspCheckRptDao pspCheckRptDao;


    @Autowired
    @Qualifier("bizEventInfoService")
    private BizEventInfoService bizEventInfoService;


    @Override
    public void pass(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("检查登记通过流水号" + bizSerno);
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                PspCheckTaskApp vo = new PspCheckTaskApp();
                vo.setBizSerno(bizSerno);
                PspCheckTaskApp pspCheckTaskApp = pspCheckTaskAppDao.queryByPk(vo);
                PspCheckTaskAppVO qvo = new PspCheckTaskAppVO();
                beanCopy(pspCheckTaskApp, qvo);
                if (Objects.nonNull(qvo)) {
                    qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                    qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                    // 审批用户编号
                    qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
                    // 审批意见
                    qvo.setAprvComment(pageApproveVo.getAprvComment());
                    // 审批状态 = 通过
                    qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_03);
                    // 审批时间
                    qvo.setAprvDate(TimeUtil.getCurrentDate());
                }
                beanCopy(qvo, pspCheckTaskApp);
                pspCheckTaskAppDao.updateByPk(pspCheckTaskApp);

                // 更新检查登记信息表 修改登记状态为“已登记”
                PspSpecCheckCus pspSpecCheckCus = new PspSpecCheckCus();
                pspSpecCheckCus.setBizSerno(pspCheckTaskApp.getChkTaskNo());
                pspSpecCheckCus.setCusId(pspCheckTaskApp.getCusId());
                PspSpecCheckCus qv =  pspSpecCheckCusDao.queryByCusIdBizSerno(pspSpecCheckCus);
                if(Objects.isNull(qv)){
                    logger.error("查询到的检查登记为空！");
                    return;
                }
                qv.setChkInputSts(RiskwarningConstant.STD_ZX_CHK_STATE_01);
                pspSpecCheckCusDao.updateByPk(qv);

                // rpt
                PspCheckRpt pspCheckRpt = new PspCheckRpt();
                pspCheckRpt.setPkId(UUIDUtil.getUUID());
                pspCheckRpt.setBizSerno(pspCheckTaskApp.getChkTaskNo());
                pspCheckRpt.setChkName(pspCheckTaskApp.getChkName());
                pspCheckRpt.setCusId(pspCheckTaskApp.getCusId());
                pspCheckRpt.setCusName(pspCheckTaskApp.getCusName());
                pspCheckRpt.setCertType(pspCheckTaskApp.getCertType());
                pspCheckRpt.setCertCode(pspCheckTaskApp.getCertCode());
                pspCheckRpt.setOwnCdt(pspCheckTaskApp.getOwnCdt());
                pspCheckRpt.setLoanTotalBln(pspCheckTaskApp.getLoanTotalBln());
                pspCheckRpt.setOthUnFactor(pspCheckTaskApp.getOthUnFactor());
                pspCheckRpt.setExpSugg(pspCheckTaskApp.getExpSugg());
                pspCheckRpt.setChkState(RiskwarningConstant.STD_ZB_CHK_STATE_09);
                pspCheckRpt.setCusManager(pspCheckTaskApp.getCusManager());
                pspCheckRpt.setInputBrId(pspCheckTaskApp.getInputBrId());
                pspCheckRpt.setMainBrId(pspCheckTaskApp.getMainBrId());
                pspCheckRpt.setCreateUser(pspCheckTaskApp.getCreateUser());
                pspCheckRpt.setCreateTime(pspCheckTaskApp.getCreateTime());
                pspCheckRptDao.insertPspCheckRpt(pspCheckRpt);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void refuse(String bizSerno, PageApproveVO pageApproveVo) {
        logger.info("检查登记拒绝流水号" + bizSerno);
        try {
            if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                PspCheckTaskApp vo = new PspCheckTaskApp();
                vo.setBizSerno(bizSerno);
                PspCheckTaskApp pspCheckTaskApp = pspCheckTaskAppDao.queryByPk(vo);
                PspCheckTaskAppVO qvo = new PspCheckTaskAppVO();
                beanCopy(pspCheckTaskApp, qvo);
                if (Objects.nonNull(qvo)) {
                    qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                    qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                    // 审批用户编号
                    qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
                    // 审批意见
                    qvo.setAprvComment(pageApproveVo.getAprvComment());
                    // 审批状态 = 拒绝
                    qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_04);
                    // 审批时间
                    qvo.setAprvDate(TimeUtil.getCurrentDate());
                }
                beanCopy(qvo, pspCheckTaskApp);
                pspCheckTaskAppDao.updateByPk(pspCheckTaskApp);

                // 更新检查登记信息表 修改登记状态为“登记作废”
                PspSpecCheckCus pspSpecCheckCus = new PspSpecCheckCus();
                pspSpecCheckCus.setBizSerno(pspCheckTaskApp.getChkTaskNo());
                pspSpecCheckCus.setCusId(pspCheckTaskApp.getCusId());
                PspSpecCheckCus qv =  pspSpecCheckCusDao.queryByCusIdBizSerno(pspSpecCheckCus);
                if(Objects.isNull(qv)){
                    logger.error("查询到的检查登记为空！");
                    return;
                }
                qv.setChkInputSts(RiskwarningConstant.STD_ZX_CHK_STATE_03);
                pspSpecCheckCusDao.updateByPk(qv);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    public void repulse(String bizSerno, PageApproveVO pageApproveVo) {
        try {
            if (bizEventInfoService.isFirstNode(bizSerno)) {
                logger.info("检查登记打回流水号" + bizSerno);
                if (Objects.nonNull(bizSerno) && !"".equals(bizSerno)) {
                    PspCheckTaskApp vo = new PspCheckTaskApp();
                    vo.setBizSerno(bizSerno);
                    PspCheckTaskApp pspCheckTaskApp = pspCheckTaskAppDao.queryByPk(vo);
                    PspCheckTaskAppVO qvo = new PspCheckTaskAppVO();
                    beanCopy(pspCheckTaskApp, qvo);
                    if (Objects.nonNull(qvo)) {
                        qvo.setLastUpdateTime(TimeUtil.getCurrentDateTime());
                        qvo.setLastUpdateUser(pageApproveVo.getAprvUserId());
                        // 审批用户编号
                        qvo.setAprvUserCode(pageApproveVo.getAprvUserId());
                        // 审批意见
                        qvo.setAprvComment(pageApproveVo.getAprvComment());
                        // 审批状态 = 打回
                        qvo.setApproveStatus(ConsoleConstant.APRV_STATUS_05);
                        // 审批时间
                        qvo.setAprvDate(TimeUtil.getCurrentDate());
                    }
                    beanCopy(qvo, pspCheckTaskApp);
                    pspCheckTaskAppDao.updateByPk(pspCheckTaskApp);

                    // 更新检查登记信息表 修改登记状态为“登记作废”
                    PspSpecCheckCus pspSpecCheckCus = new PspSpecCheckCus();
                    pspSpecCheckCus.setBizSerno(pspCheckTaskApp.getChkTaskNo());
                    pspSpecCheckCus.setCusId(pspCheckTaskApp.getCusId());
                    PspSpecCheckCus qv =  pspSpecCheckCusDao.queryByCusIdBizSerno(pspSpecCheckCus);
                    if(Objects.isNull(qv)){
                        logger.error("查询到的检查登记为空！");
                        return;
                    }
                    qv.setChkInputSts(RiskwarningConstant.STD_ZX_CHK_STATE_03);
                    pspSpecCheckCusDao.updateByPk(qv);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
